package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.*;
import com.sky.entity.Employee;
import com.sky.entity.Orders;
import com.sky.entity.WorkOrder;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.service.OrderService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private OrderService orderService;
//    @Autowired
//    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("用户登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

//        //登录成功后，生成jwt令牌
//        Map<String, Object> claims = new HashMap<>();
//        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
//        String token = JwtUtil.createJWT(
//                jwtProperties.getAdminSecretKey(),
//                jwtProperties.getAdminTtl(),
//                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUserid())
                .name(employee.getName())
               // .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

@PostMapping
@ApiOperation("注册用户")
    public Result save(@RequestBody UserDTO userDTO){
        log.info("新增用户操作",userDTO);
        employeeService.save(userDTO);
        return Result.success(userDTO);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询信息")
   public Result<Employee> getByid(@PathVariable Long id){
       Employee employee= employeeService.getbyid(id);
        return Result.success(employee);
    }

    @PutMapping
    @ApiOperation("修改个人信息")
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        log.info("编辑员工信息",employeeDTO);
       employeeService.update(employeeDTO);
    return Result.success();
    }
    @PostMapping("/submit")
    @ApiOperation("学生用户报修单 ")
    public Result<Orders> submit(@RequestBody OrdersDTO ordersDTO)
    {
        log.info("用户产生订单",ordersDTO);
        Orders orders= orderService.submitOrder(ordersDTO);
        return Result.success(orders);
    }
    @GetMapping("/{status}")
    @ApiOperation("根据订单状态查询订单")
    public Result<PageResult> selectUserOrders (@PathVariable statusOrder statusOrder){
        log.info("用户查询订单",statusOrder);
        PageResult pageResult = orderService.selectUserOrder(statusOrder);
        return Result.success(pageResult);
    }
    @PutMapping("/reminder/{id}")
    @ApiOperation("客户催单")
    public Result reminder(@PathVariable("id") Long id){
        orderService.remind(id);
        return Result.success();
    }
}
